/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.synnapps.carouselview;

import ohos.agp.components.AttrSet;

import ohos.app.Context;

import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * xml 属性读取工具类
 *
 * @since 2021-04-07
 */
public class AttrValue {
    private AttrValue() {
    }

    /**
     * 获取int类型值
     *
     * @param attrSet      属性集
     * @param name         属性名称
     * @param defaultValue 默认值
     * @return int类型值
     */
    public static Integer getInteger(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getIntegerValue();
        }
        return value;
    }

    /**
     * 获取float类型值
     *
     * @param attrSet      属性集
     * @param name         属性名称
     * @param defaultValue 默认值
     * @return float类型值
     */
    public static float getFloat(AttrSet attrSet, String name, float defaultValue) {
        float value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getFloatValue();
        }
        return value;
    }

    /**
     * 获取boolean类型值
     *
     * @param attrSet      属性集
     * @param name         属性名称
     * @param defaultValue 默认值
     * @return boolean类型值
     */
    public static boolean getBoolean(AttrSet attrSet, String name, boolean defaultValue) {
        boolean value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getBoolValue();
        }
        return value;
    }

    /**
     * 获取颜色值
     *
     * @param attrSet      属性集
     * @param name         属性名称
     * @param defaultValue 默认值
     * @return color颜色值
     */
    public static int getColor(AttrSet attrSet, String name, int defaultValue) {
        int value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getColorValue().getValue();
        }
        return value;
    }

    /**
     * 读取 xml 尺寸型属性, 例如 cornerRadius="4vp"
     *
     * @param context      上下文
     * @param dimensionRes 属性键
     * @return 属性值
     */
    public static int getDimension(Context context, int dimensionRes) {
        float dimension = 0;
        try {
            dimension = context.getResourceManager().getElement(dimensionRes).getFloat();
        } catch (NotExistException | WrongTypeException | IOException e) {
            e.printStackTrace();
        }
        return (int) dimension;
    }
}
